package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    //新增菜品
    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        //1.构造菜品基本信息数据，将其存入dish表中
        Dish dish = new Dish();
        //拷贝属性值
        BeanUtils.copyProperties(dishDTO,dish);
        //调用mapper层
        dishMapper.addDish(dish);
        log.info("检查数据{}",dish.getId());
        //2.构造菜品口味表数据，将其存入dish_flavor表中
        List<DishFlavor> dishFlavor = dishDTO.getFlavors();
        //2.1关联菜品id
        dishFlavor.forEach(flavor->{
            flavor.setDishId(dish.getId());
        });
        //2.2调用mapper
        dishFlavorMapper.addFlavor(dishFlavor);
    }
    //菜品分页分页查询
    @Override
    public PageResult page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page= dishMapper.page(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }
    //删除菜品
    @Override
    @Transactional//开启事务
    public void deleteDish(List<Long> ids) {
        //1.启售中不允许删除
        ids.forEach(id->{
           Dish dish= dishMapper.selectById(id);
           if(dish.getStatus()== StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        });
        //2.菜品被套餐不允许删除
       Integer count= setmealDishMapper.countByDishId(ids);
       if(count>0){
           throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
       }
        //3.菜品删除关联的口味也要删除
        dishMapper.deleteBatch(ids);
        dishFlavorMapper.deleteBatch(ids);
    }
    //根据id查询菜品
    @Override
    public DishVO getById(Long id) {
        //查询菜品数据
       Dish dish= dishMapper.selectById(id);
       //查询口味信息
      List<DishFlavor>  flavor= dishFlavorMapper.getByDishId(id);
        //构造DishVO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors( flavor);
        return dishVO;
    }
    @Transactional
    @Override
    //修改菜品
    public void update(DishDTO dto) {
        //修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);
        //修改口味(先删后增)
        List<Long> list=new LinkedList<>();
        list.add(dish.getId());
         dishFlavorMapper.deleteBatch(list);
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            //关联菜品id
            flavors.forEach(flavor->{
                flavor.setDishId(dish.getId());
            });
            dishFlavorMapper.addFlavor(flavors);
        }

    }
    //根据分类id查询菜品
    @Override
    public List<DishVO> getByCategoryId(Long categoryId) {
        return dishMapper.getByCategoryId(categoryId);

    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    //菜品起售停售
    @Override
    public void updateDihsStauts(Long status, Long id) {
        dishMapper.updateDishStatus(status,id);
    }

}
